package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivityGuideBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.MainActivity;
import com.love_weinuo.ui.fragment.ApplicationState;
import com.love_weinuo.ui.fragment.BaseFragmentAdapter;
import com.love_weinuo.ui.fragment.PageFragment;
import com.love_weinuo.util.rx.RxActivityTool;

/**
 * Created by sensyang.
 * on 2018/10/8 0008.
 * Email:532245792@qq.com
 * desc:引导界面
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    ActivityGuideBinding guideBinding;
    public ViewPager mViewPager;
    public RadioButton mRb1, mRb2, mRb3 ,mRb4;
    public TextView mBtn;
    public TextView text;
    private int mViewPagerCount = 0;
    RelativeLayout relativeLayout;
    private Fragment fragment[] = new Fragment[5];
    private ApplicationState applicationState = new ApplicationState();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        guideBinding = DataBindingUtil.setContentView(this, R.layout.activity_guide);
        guideBinding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        relativeLayout = findViewById(R.id.relative);
        mViewPager = findViewById(R.id.view_pager);
        mBtn = findViewById(R.id.jumpToMain);
        mRb1 = findViewById(R.id.rb1);
        mRb2 = findViewById(R.id.rb2);
        mRb3 = findViewById(R.id.rb3);
        mRb4 = findViewById(R.id.rb4);
        text = findViewById(R.id.comeToMain);
        mBtn.setOnClickListener(this);
        text.setOnClickListener(this);
        mRb1.setChecked(true);

        BaseFragmentAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager());
        fragment[0] = new PageFragment(0);
        fragment[1] = new PageFragment(1);
        fragment[2] = new PageFragment(2);
        fragment[3] = new PageFragment(3);
        fragmentAdapter.addData(fragment);
        guideBinding.viewPager.setAdapter(fragmentAdapter);
        guideBinding.viewPager.setCurrentItem(mViewPagerCount);
        guideBinding.viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.comeToMain:
                startActivity(new Intent(this, GuideActivity.class));
                RxActivityTool.skipActivityAndFinish(GuideActivity.this,LoginActivity.class);
                break;
            case R.id.jumpToMain:
                startActivity(new Intent(this, GuideActivity.class));
                RxActivityTool.skipActivityAndFinish(GuideActivity.this,LoginActivity.class);
                break;
            default:
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (applicationState.isMainSliding() && position == 1) {
            applicationState.setMainSliding(false);
        }

        if (0 == position) {
            guideBinding.rb1.setVisibility(View.VISIBLE);
            guideBinding.rb2.setVisibility(View.VISIBLE);
            guideBinding.rb3.setVisibility(View.VISIBLE);
            guideBinding.rb4.setVisibility(View.VISIBLE);
            guideBinding.rb1.setChecked(true);
            guideBinding.rb2.setChecked(false);
            guideBinding.rb3.setChecked(false);
            guideBinding.rb4.setChecked(false);
            guideBinding.comeToMain.setVisibility(View.GONE);
        }else if(1 == position){
            guideBinding.rb1.setVisibility(View.VISIBLE);
            guideBinding.rb2.setVisibility(View.VISIBLE);
            guideBinding.rb3.setVisibility(View.VISIBLE);
            guideBinding.rb4.setVisibility(View.VISIBLE);
            guideBinding.rb1.setChecked(false);
            guideBinding.rb2.setChecked(true);
            guideBinding.rb3.setChecked(false);
            guideBinding.rb4.setChecked(false);
            guideBinding.comeToMain.setVisibility(View.GONE);
        }else if(2 == position){
            guideBinding.rb1.setVisibility(View.VISIBLE);
            guideBinding.rb2.setVisibility(View.VISIBLE);
            guideBinding.rb3.setVisibility(View.VISIBLE);
            guideBinding.rb4.setVisibility(View.VISIBLE);
            guideBinding.rb1.setChecked(false);
            guideBinding.rb2.setChecked(false);
            guideBinding.rb3.setChecked(true);
            guideBinding.rb4.setChecked(false);
            guideBinding.comeToMain.setVisibility(View.GONE);
        }else if(3 == position){
            guideBinding.rb1.setVisibility(View.GONE);
            guideBinding.rb2.setVisibility(View.GONE);
            guideBinding.rb3.setVisibility(View.GONE);
            guideBinding.rb4.setVisibility(View.GONE);
            guideBinding.comeToMain.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (!applicationState.isMainSliding() && state != ViewPager.SCROLL_STATE_IDLE) {
            applicationState.setMainSliding(true);
        }
    }
}
